package data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import perspectives.DataSource;
import perspectives.Property;
import perspectives.PropertyManager.Options;

/**
 *
 * @author mershack
 */
public class TableData extends DataSource {

    protected TableDistances table;
    boolean valid;
    
    

    public TableData(String name) {
        super(name);

        valid = false;
        

        table = new TableDistances();
        
        createProperties();
        
    }
    
    protected void createProperties()
    {

        try {

            OpenFile f = new OpenFile();
            f.dialogTitle = "Open Data File";
            f.extensions = new String[3];
            f.extensions[0] = "*";
            f.extensions[1] = "txt";
            f.extensions[2] = "xml";



            Property<Boolean> p0 = new Property<Boolean>("Network CSV File");
            p0.setValue(true);
            addProperty(p0);


            Property<Boolean> p2 = new Property<Boolean>("Col Headers?");
            p2.setValue(true);
            this.addProperty(p2);
            
            Property<Boolean> p21 = new Property<Boolean>("Row Headers?");
            p21.setValue(false);
            this.addProperty(p21);

            Property<Options> p3 = new Property<Options>("Delimiter");
            Options o = new Options();
            o.options = new String[3];
            o.options[0] = "COMMA";
            o.options[1] = "TAB";
            o.options[2] = "SPACE";
            
            o.selectedIndex = 0;
            p3.setValue(o);
            this.addProperty(p3);

            Property<OpenFile> p1 = new Property<OpenFile>("Tabular File");
            p1.setValue(f);
            addProperty(p1);




        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public <T> void propertyUpdated(Property p, T newvalue) {
        boolean cvs = (Boolean) getProperty("Network CSV File").getValue();
        
      /*  if (cvs) {
            //se;
            this.removeProperty("Col Headers?");
            this.removeProperty("Row Headers?");
            this.removeProperty("Delimiter");

        }*/

        if (p.getName() == "Tabular File") {

           if(!cvs){
                boolean ch = (Boolean) getProperty("Col Headers?").getValue();
                boolean rh = (Boolean) getProperty("Row Headers?").getValue();

                

              //hide the others if it is a json file

             int delim = ((Options) (getProperty("Delimiter").getValue())).selectedIndex;
             OpenFile f = ((OpenFile) newvalue);
             String d = ",";   //default selection
             if (delim == 1) {
                d = "\t";
              } else if (delim == 2) {
                d = " ";
              }
             
          

              table.fromFile(f.path, d, ch, rh);  //Function to Get the Data from File
           }
           else{   //get the data from a CSV file
                boolean ch = (Boolean) getProperty("Col Headers?").getValue();
                boolean rh = (Boolean) getProperty("Row Headers?").getValue();

          
             int delim = ((Options) (getProperty("Delimiter").getValue())).selectedIndex;
             OpenFile f = ((OpenFile) newvalue);
            String d = ",";   //default selection
             if (delim == 1) {
                d = "\t";
              } else if (delim == 2) {
                d = " ";
              }

              //table.fromFile(f.path, d, ch, rh);  //Function to Get the Data from File
                 System.out.println("delim  "+d);
              table.fromNetworkCSVFile(f.path, d, ch);  //Function to Get the Data from File
               
           } 
            //Determine the properties of the file such as number of rows and columns and print them on the screen
            if (table.getColumnCount() != 0) {
                this.setLoaded(true);

                this.removeProperty("Tabular File");
                this.removeProperty("Delimiter");
                this.removeProperty("Col Headers?");                
                this.removeProperty("Network CSV File");
                this.removeProperty("Row Headers?");

                try {
                    Property<Integer> p1 = new Property<Integer>("# Columns");
                    p1.setValue(table.getColumnCount());
                    p1.setReadOnly(true);
                    this.addProperty(p1);

                    Property<Integer> p2 = new Property<Integer>("# Rows");
                    p2.setValue(table.getRowCount());
                    p2.setReadOnly(true);
                    this.addProperty(p2);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /* Method to get the table */
    public TableDistances getTable() {
        return table;
    }

    /* method to set that the table is valid */
    public boolean isValid() {
        return valid;
    }
    
  
    
    

}
